<template>
  <view class="setting-page">
    <tui-list-view class="margin-t">
      <tui-list-cell padding="14px 15px" :size="16" arrow>
        <view class="item"> 账号安全中心 </view>
      </tui-list-cell>
      <tui-list-cell
        padding="14px 15px"
        :size="16"
        arrow
        @click="toNav('language')"
      >
        <view class="item"> 多语言 </view>
      </tui-list-cell>
    </tui-list-view>
    <tui-list-view class="margin-t">
      <tui-list-cell
        padding="14px 15px"
        :size="16"
        arrow
        @click="toNav('notice')"
      >
        <view class="item-between">
          消息通知
          <!-- #ifdef APP-PLUS -->
          <view class="to-set" v-if="!openPush">
            <tui-icon
              name="about-fill"
              size="18"
              unit="px"
              color="#FEAF00"
              class="icon"
            />
            前往“设置”中开启
          </view>
          <!-- #endif -->
        </view>
      </tui-list-cell>
      <tui-list-cell padding="14px 15px" :size="16" arrow>
        <view class="item"> 关于 </view>
      </tui-list-cell>
    </tui-list-view>
    <tui-list-view class="margin-t">
      <tui-list-cell
        padding="14px 15px"
        :size="16"
        :arrow="true"
        @click="showClear = true"
      >
        <view class="item-between">
          清理缓存 <text class="to-set">{{ storageTitle }}</text>
        </view>
      </tui-list-cell>
    </tui-list-view>

    <tui-list-view class="margin-t">
      <tui-list-cell
        padding="14px 15px"
        :size="16"
        :arrow="false"
        @click="showLogout = true"
      >
        <view class="item-center logout"> 退出登录 </view>
      </tui-list-cell>
    </tui-list-view>

    <tui-actionsheet
      :show="showClear"
      :item-list="clearItemList"
      tips="本地缓存内容将被清理"
      @click="clearClick"
      @cancel="showClear = false"
    >
    </tui-actionsheet>

    <tui-actionsheet
      :show="showLogout"
      :item-list="logoutItemList"
      tips="退出登录会清除您的登录信息，确认退出吗？"
      @click="logoutClick"
      @cancel="showLogout = false"
    >
    </tui-actionsheet>
  </view>
</template>

<script>
import permision from "@/common/js/permission.js";
export default {
  data() {
    return {
      isIOS: false,
      openPush: false,
      storageTitle: "",
      showClear: false,
      clearItemList: [
        {
          text: "清理缓存",
          color: "#ed4014",
        },
      ],
      showLogout: false,
      logoutItemList: [
        {
          text: "确认退出",
          color: "#ed4014",
        },
      ],
    };
  },
  mounted() {
    this.init();
  },
  onShow() {
    this.judgePermission();
  },
  methods: {
    init() {
      this.judgePermission();
      let res = uni.getStorageInfoSync();
      this.storageTitle =
        (res.currentSize / Math.pow(1024.0, 2)).toFixed(2) + " MB";
    },
    judgePermission() {
      // #ifdef APP-PLUS
      this.isIOS = plus.os.name == "iOS";
      if (this.isIOS) {
        this.openPush = permision.judgeIosPermission("push");
      } else {
        this.openPush = permision.requestAndroidPermission("push");
      }
      // #endif
    },
    toNav(v) {
      if (v == "language") {
        uni.navigateTo({
          url: "/pages/setting/language/language",
        });
      } else if (v == "notice") {
        this.judgePermission();
        // #ifdef APP-PLUS
        if (!this.openPush) {
          permision.gotoAppPermissionSetting();
        }
        // #endif
      }
    },
    clearClick(v) {
      if (v.index == 0) {
        this.showClear = false;
        this.showLoading("清除中");
        this.storageTitle = "清除中...";
        // 用户等信息不清空
        let accessToken = uni.getStorageSync("accessToken");
        let userInfo = uni.getStorageSync("userInfo");
        uni.clearStorageSync();
        uni.setStorageSync("accessToken", accessToken);
        uni.setStorageSync("userInfo", userInfo);
        this.storageTitle = "0.00 MB";
        uni.hideLoading();
      }
    },
    logoutClick(v) {
      if (v.index == 0) {
        this.showLoading("请稍后...");
        uni.clearStorageSync();
        uni.hideLoading();
        this.isLogin();
      }
    },
  },
};
</script>

<style lang="scss" scoped>
@import "./setting.scss";
</style>
